"""
 * Add Fulfilment tables

Revision ID: f690787df65
Revises: 3b1b405d632e
Create Date: 2013-01-13 13:28:59.186275

"""

# revision identifiers, used by Alembic.
revision = 'f690787df65'
down_revision = '3b1b405d632e'

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('fulfilment_group',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('person_id', sa.Integer(), nullable=True),
    sa.Column('code', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['person_id'], ['person.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('code')
    )
    op.create_table('fulfilment_status',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.Text(), nullable=False),
    sa.Column('locked', sa.Boolean(), nullable=False),
    sa.Column('void', sa.Boolean(), nullable=False),
    sa.Column('completed', sa.Boolean(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('fulfilment_type',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.Text(), nullable=False),
    sa.Column('initial_status_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['initial_status_id'], ['fulfilment_status.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('fulfilment',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('person_id', sa.Integer(), nullable=False),
    sa.Column('type_id', sa.Integer(), nullable=False),
    sa.Column('status_id', sa.Integer(), nullable=False),
    sa.Column('creation_timestamp', sa.DateTime(), nullable=False),
    sa.Column('last_modification_timestamp', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['person_id'], ['person.id'], ),
    sa.ForeignKeyConstraint(['status_id'], ['fulfilment_status.id'], ),
    sa.ForeignKeyConstraint(['type_id'], ['fulfilment_type.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('fulfilment_group_map',
    sa.Column('fulfilment_id', sa.Integer(), nullable=False),
    sa.Column('fulfilment_group_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['fulfilment_group_id'], ['fulfilment_group.id'], onupdate='CASCADE', ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['fulfilment_id'], ['fulfilment.id'], onupdate='CASCADE', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('fulfilment_id', 'fulfilment_group_id')
    )
    op.create_table('fulfilment_type_status_map',
    sa.Column('fulfilment_type_id', sa.Integer(), nullable=False),
    sa.Column('fulfilment_status_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['fulfilment_status_id'], ['fulfilment_status.id'], onupdate='CASCADE', ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['fulfilment_type_id'], ['fulfilment_type.id'], onupdate='CASCADE', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('fulfilment_type_id', 'fulfilment_status_id')
    )
    op.create_table('fulfilment_item',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('fulfilment_id', sa.Integer(), nullable=False),
    sa.Column('product_id', sa.Integer(), nullable=False),
    sa.Column('product_text', sa.Text(), nullable=True),
    sa.Column('qty', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['fulfilment_id'], ['fulfilment.id'], ),
    sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('product_text')
    )
    op.add_column(u'product', sa.Column('fulfilment_type_id', sa.Integer(), sa.ForeignKey('fulfilment_type.id'), nullable=True))
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column(u'product', 'fulfilment_type_id')
    op.drop_table('fulfilment_item')
    op.drop_table('fulfilment_type_status_map')
    op.drop_table('fulfilment_group_map')
    op.drop_table('fulfilment')
    op.drop_table('fulfilment_type')
    op.drop_table('fulfilment_status')
    op.drop_table('fulfilment_group')
    ### end Alembic commands ###
